import {
  reqCellPhoneByCode,
  reqCellPhoneByPassword,
  reqDetail,
  reqLogout,
} from "@/api";
import { setCookie, getCookie, removeCookie } from "@/utils/cookie";
import { setUserId, getUserId, removeUserId } from "@/utils/userId";
const state = {
  code: 0,
  //   cookie
  cookie: getCookie(),
  //   登录账号
  userId: getUserId(),
  //   用户信息
  userInfo: {},
  // 显示登录同意模块
  isShowLoginAgreeModal: false,
};
const mutations = {
  UPDATETOKEN(state, cookie) {
    setCookie(cookie);
    state.cookie = cookie;
  },
  UPDATEUSERINFO(state, userId) {
    setUserId(userId);
    state.userId = userId;
  },
  GETUSERINFO(state, userInfo) {
    state.userInfo = userInfo;
  },
  EXIT(state) {
    state.userInfo = {};
    state.userId = "";
    state.token = "";
    removeCookie();
    removeUserId();
  },
  changIsShowLoginAgreeModal(state, payload) {
    state.isShowLoginAgreeModal = payload;
  },
};
const actions = {
  //二维码更新Token
  updateToken({ commit }, cookie) {
    commit("UPDATETOKEN", cookie);
  },
  //手机验证码更新Token
  async updateTokenWithCode({ commit }, { phone, code }) {
    try {
      let result = await reqCellPhoneByCode(phone, code);
      if (result.code == 200) {
        commit("UPDATETOKEN", result.cookie);
        commit("UPDATEUSERINFO", result.profile);
      }
    } catch (error) {
      alert(error);
    }
  },
  //密码更新Token
  async updateTokenWithPassword({ commit }, { phone, password }) {
    try {
      let result = await reqCellPhoneByPassword(phone, password);
      if (result.code == 200) {
        commit("UPDATETOKEN", result.cookie);
        commit("UPDATEUSERINFO", result.account.id);
      }
    } catch (error) {
      alert(error);
    }
  },
  //获取用户信息
  async getUserInfo({ commit }, id) {
    let result = await reqDetail(id, localStorage.getItem("COOKIE"));
    commit("GETUSERINFO", result);
  },
  //退出登录
  async exit({ commit }) {
    await reqLogout();
    commit("EXIT");
  },
};
const getters = {};

export default {
  state,
  mutations,
  actions,
  getters,
};
